package com.lingz.manager.modules.wuxue.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingz.manager.modules.wuxue.entity.HtjrDeviceInfo;
import com.lingz.manager.modules.wuxue.mapper.HtjrDeviceInfoMapper;
import com.lingz.manager.modules.wuxue.service.HtjrDeviceInfoService;
import com.lingz.manager.modules.wuxue.utils.DeviceDataUtil;
import com.lingz.manager.modules.wuxue.utils.SendRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (HdjrDeviceInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-12-14 17:09:39
 */
@Service("hdjrDeviceInfoService")
public class HtjrDeviceInfoServiceImpl extends ServiceImpl<HtjrDeviceInfoMapper, HtjrDeviceInfo> implements HtjrDeviceInfoService {

    @Value("${htjr}")
    String htjr;

    @Autowired
    HtjrDeviceInfoMapper htjrDeviceInfoMapper;

    @Override
    public void requestHtjrDeviceInfoDataToDB() {
        SendRequest request = new SendRequest();
        String jsonResponse = request.sendGet(htjr);
        JSONObject responseJson = JSONObject.parseObject(jsonResponse);
        if (responseJson != null && responseJson.containsKey("data")){
            JSONArray data = responseJson.getJSONArray("data");
            List<HtjrDeviceInfo> dataInfos = new ArrayList<>();
            if(data!= null && !data.isEmpty()){
                for(int i = 0 ; i <data.size(); i++){
                    JSONObject jsonObject = data.getJSONObject(i);
                    HtjrDeviceInfo htjrDeviceInfo = DeviceDataUtil.parseJsonToHtjrDeviceInfoData(jsonObject);
                    dataInfos.add(htjrDeviceInfo);
                }
            }
            htjrDeviceInfoMapper.insertBatchSomeColumn(dataInfos);
        }
    }

    @Override
    public List<HtjrDeviceInfo> findHtjrDeviceInfoList() {
        LambdaQueryWrapper<HtjrDeviceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(HtjrDeviceInfo::getId);
        return baseMapper.selectList(queryWrapper);
    }
}

